#include "mfqueue.h"

MFQueue::MFQueue() {
	list<Process> queue;
	vector<int> ran;
	int time_quantum = 0;
	int run_for = 0;
}

MFQueue::MFQueue(int quantum) {
	list<Process> queue;
	vector<int> ran;
	int time_quantum = quantum;
	int run_for = 0;
}

MFQueue::~MFQueue() {
}

Process MFQueue::run_next_process(bool run) {
	Process result;
	
	if(run) {
		queue.front().run_for(1);
		ran.push_back(queue.front().get_pid());
		run_for++;
		if((time_quantum - run_for) <= 0 || queue.front().is_done()) {
			result.clone(Process(queue.front()));
			queue.pop_front();
			run_for = 0;
		}
	} else {
		ran.push_back(-1);
	}
	
	return result;
}

void MFQueue::print_gantt() {
	int previous = -1;
	
	for(int i = 0; i <= (int)ran.size(); i++) {
		if(i < (int)ran.size()) {
			if(ran[i] != previous) {
				if(previous != -1) {
					cout << " [" << previous << "] " << i;
				}
				previous = ran[i];
			}
		} else if(i != 0 && (ran[i - 1] == previous) && (previous != -1)) {
			cout << " [" << previous << "] " << i;
		} else if(i != 0 && (ran[i - 1] != -1)) {
			cout << " [" << ran[i - 1] << "] " << i;
		}
	}
}

void MFQueue::add_process(Process p) {
	queue.push_back(p);
}

void MFQueue::add_processes(list<Process> p) {
	list<Process>::iterator itr;
	for(itr = p.begin(); itr != p.end(); itr++) {
		queue.push_back(*itr);
	}
}

bool MFQueue::contains(Process p) {
	list<Process>::iterator itr;
	itr = find(queue.begin(), queue.end(), p);
	
	return (p == *itr);;
}

bool MFQueue::is_empty() {
	return queue.empty();
}

int MFQueue::get_time_quantum() {
	return time_quantum;
}
